import Guid from "@/common/guid";
import {handleGetVoiceTypeItem} from "@/common/Voice";
import useVoiceTaskStore from "@/store/useVoiceTaskStore";

const voiceTaskStore = useVoiceTaskStore();

const handleMessage = (event: any) => {
  console.log("event========>", event);
  let messageObject = JSON.parse(JSON.parse(event)?.Payload);
  console.log("messageObject", messageObject);
  if (!!messageObject && messageObject?.messageType != 'terminalHeartbeat') {

    let enable = true;
    let autoPrint = true;
    console.log("messageObject?.messageType", messageObject?.messageType);
    if (enable) {
      addVoiceTaskFn(Guid.NewGuid().ToString(), messageObject?.messageType)
    }
    /*if (['newOrder', 'newPreOrder', 'orderCancelApply', 'orderRefundApply'].includes(messageObject.messageType)) {
      if (messageObject.message.length && autoPrint) {
        //let idList = messageObject.message?.map(v => v.orderNo || v.afsOrderNo)
        let a;
        if (messageObject?.message?.length && messageObject.message[0].orderNo) {
          await Promise.all(a?.map(async v => {
            await addPrintTaskFn(v, 'order', true)
          }))
        }
        if (messageObject?.message?.length && messageObject.message[0].afsOrderNo) {
          await Promise.all(a?.map(async v => {
            await addPrintTaskFn(v, 'afs', true)
          }))
        }
      }
    }*/
  }
}
const addVoiceTaskFn = (id: number, v: any) => {
  let voice = handleGetVoiceTypeItem(v)
  console.log("voice", voice);
  if (!!voice) {
    voiceTaskStore.addVoiceTask({
      id,
      path: 'static/audio/default/' + voice.file,
      times: 1
    })
  }
}

/*const addPrintTaskFn = function (v: any, type: 'order' | '|afs' = 'order', isAutoPrint = false) {
  printTaskStore.addTask({
    id: type == 'order' ? v.orderNo : v.afsOrderNo,
    task: async () => await handleTaskPrint(v, type, isAutoPrint)
  })
}

const handleTaskPrint =  async (order: any, type: 'order' | 'afs', isAutoPrint:boolean = false) => {
  return new Promise((resolve) => {
    let data = getPrintData(order, type, printTimes.value), skip = isAutoPrint ? '0' : (isSkip.value ? '1' : '0');
    console.log(order, 'data')
    handlePreQueryOrderPrint([order.orderNo]);
    BluetoothPrinter.setDevice(bluetoothPrinter).checkAndPrint(data).then((res:any) => {
      if (res.code == '0') {
        if (type == 'afs') {
          // ConfirmAfsOrderPrint(order, skip)
        } else {
          // ConfirmOrderPrint(order, skip)
        }
      }
      resolve()
    }).catch(res => {
      if (!isAutoPrint) {
        //skipFunc(res?.msg);
      }
    })
  })
}
const handlePreQueryOrderPrint = async (idList:any) => {
  let params:any = {
    orderNoList: idList,
    operateMode: '自动',
    operateTerminal: 'APP'
  }
  const response:any = await bcmRequest(bcmUrl.orderApi, '订单表', 'PreQueryOrderAutoPrint', params);
  let arr:any[] = []
  response?.filter(v => v.status == '1')?.map(v => {
    arr.push(v.bcmOrder)
  })
  return arr
}
const handlePreQueryAfsOrderPrint = async function (idList:any) {
  let params:any = {
    afsOrderNoList: idList,
    operateMode: '自动',
    operateTerminal: 'APP'
  }
  const response:any = await bcmRequest(bcmUrl.orderApi, '售后单表', 'PreQueryAfsOrderAutoPrint', params);
  let arr:any[] = []
  response?.filter(v => v.status == '1')?.map(v => {
    arr.push(v.bcmAfsOrder)
  })
  return arr
}


const confirmAfsOrderPrint = (afsOrder: any, skipPrintFlag: string = '0') => {
  orderAction({
    uniqueKey: '售后单表',
    methodName: 'ConfirmAfsOrderPrint'
  }, afsOrder, {
    afsOrderNo: afsOrder.afsOrderNo,
    afsOrderId: afsOrder.afsOrderId,
    BillId: afsOrder.afsOrderId,
    thirdAfsOrderNo: afsOrder.thirdAfsOrderNo,
    extras: {
      operateMode: '手动',
      operateTerminal: 'APP',
      skipPrintFlag
    }
  })
}
type OrderActionCallback = (v: any) => void;

const orderAction = async (option:{uniqueKey:string, methodName:string }, v: any, params: any = undefined, text = '', popup = false, fn?:OrderActionCallback) => {
  service.enjoyShowLoading('正在加载...');
  let param = {
    chnlCode: v.chnlCode,
    chnlType: v.chnlType,
    orderNo: v.orderNo,
    storeCode: v.storeCode,
    thirdOrderNo: v.thirdOrderNo,
    orderId: v.orderId,
    ...params,
  }
  const response = await bcmRequest(bcmUrl.orderApi, option.uniqueKey, option.methodName, param);
  service.enjoyHideLoading();
  if (text) {
    uni.showToast({
      title: text,
      icon: 'none'
    });
  }
  if (popup) {
    close()
  }


  if (fn) {
    fn(v)
  }
  if (params) {
    return response
  }
}*/
export default {
  handleMessage
}